package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author mqx
 * @date 2020-10-28 11:21:13
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;
    
    //  http://activity.gmall.com/seckill.html
    @RequestMapping("seckill.html")
    public String seckillIndex(HttpServletRequest request){
        //  远程调用
        Result result = activityFeignClient.findAll();
        //  页面需要list
        request.setAttribute("list",result.getData());
        //  返回页面
        return "seckill/index";
    }
    // http://activity.gmall.com/seckill/29.html
    @GetMapping("seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,HttpServletRequest request){
        //  远程调用
        Result result = activityFeignClient.getSeckillGoods(skuId);
        //  页面需要list
        request.setAttribute("item",result.getData());

        return "seckill/item";
    }

    //  window.location.href = '/seckill/queue.html?skuId='+this.skuId+'&skuIdStr='+skuIdStr
    @GetMapping("seckill/queue.html")
    public String queue(HttpServletRequest request){

        String skuId = request.getParameter("skuId");
        String skuIdStr = request.getParameter("skuIdStr");
        //  保存数据
        request.setAttribute("skuId",skuId);
        request.setAttribute("skuIdStr",skuIdStr);
        return "seckill/queue";
    }

    //  http://activity.gmall.com/seckill/trade.html
    @GetMapping("seckill/trade.html")
    public String trade(Model model){
        //  基本用法！
        Result<Map<String, Object>> result = activityFeignClient.trade();
        model.addAllAttributes(result.getData());
        //  返回页面！
        return "seckill/trade";

        //        if (result.isOk()){
        //            model.addAllAttributes(result.getData());
        //            //  返回页面！
        //            return "seckill/trade";
        //        }else {
        //            //  后台要存储一个message
        //            model.addAttribute("message",result.getMessage());
        //            return "seckill/fail";
        //        }


    }
}
